/*
 * CRTP_ARP_table.c
 *
 *  Created on: May 18, 2013
 *      Author: ILYA
 */

#include "CRTP_ARP_table.h"
#include "mib_functions.h"

/*CRTP start*/
/*this function called when a packet has been received and send data to application layer*/
enum CRTP_STATE crtp_input(data_type *data, int poll_final_bit, int port)
{
#ifdef DEBUGING
	switch(port)
	{
	/*1(161)-snmp port, 2(162)-snmp trap port*/
	case PID_UI_FRAME | APP_GENERAL_SNMP:
	case PID_UI_FRAME | APP_TRAP_SNMP:
	{
//		char *tailOID = "01010B";//"1.1.11"; //.monitoring.radioStatusStatistics.errStatus
//		char *real_OID = (char *)malloc(strlen(picocell_snmp_OID) + strlen(tailOID) + 1) ;
//		strcpy(real_OID, picocell_snmp_OID);
//		strcat (real_OID, tailOID);
		/////////iiiiiiiiiiiiiiiiiiiiiiiiiiiiiii start for debuging
		if(data != 0)
		{
			printk("\r\ndata = %s", data->data);
			printk("\r\nsize = %d", data->size);
			printk("\r\nLinkID = %d", data->LinkID);
			printk("\r\np/f bit = %x", poll_final_bit);
			printk("\r\nport = %x", port);
		}
		else
		{
			printk("\r\nresponse is recived\r\n");
		}
		/////////iiiiiiiiiiiiiiiiiiiiiiiiiiiiii  end for debuging
		break;
	}
	default:
		break;
	}
#endif
	return SUCCESS;
}
/*this function is calling by application layer, when it need to send data*/
enum CRTP_STATE crtp_output(unsigned char *data, int data_size, int poll_final_bit, int port)
{
#ifdef DEBUGING
	switch(port)
	{
	/*161(1)-snmp port, 162(2)-snmp trap port*/
	case PID_UI_FRAME | APP_GENERAL_SNMP:
	case PID_UI_FRAME | APP_TRAP_SNMP:
	{
		///iiiiiiiiiiiiiiiiiiiiiiiiiii start for debuging
		data_type *data_st = create_data_type(data_size);
		memcpy(data_st->data, data, data_st->size);
		data_st->LinkID = 2;
		llc_output_data(data_st, poll_final_bit, PID_UI_FRAME | port);
		data_type_free(data_st);
		///iiiiiiiiiiiiiiiiiiiiiiiiiii end for debuging
		break;
	}
	default:
		break;
	}
#endif
	return SUCCESS;
}
/*CRTP end*/

/*ARP start*/
/*if j-th bit of is_arp_i_not_null equal to 1 then j-th row of arp table is empty,
 * else if  j-th bit of is_arp_i_null equal to 0 then j-th row of arp table is full*/
unsigned char *is_arp_i_not_null = NULL;
unsigned char queueing_row = 0;
TreeNode *rootOfTree;

void init_arp_table()
{
	init_treeNode(FLASH_ADDRESS, &rootOfTree);
	is_arp_i_not_null = calloc((MAX_LINK_ID/(8*sizeof(char)))+1, sizeof(char));
}
/*this function must write some fields of list_rim_frame into ARP table*/
void write_in_arp(struct ListRIMFrames list_rim_frame)
{
	/*
	//this function must write some fields of list_rim_frame into ARP table, into row list_rim_frame->linkID
	//the fields are
	//1. list_rim_frame.Link_ID
	//2. list_rim_frame.RIMFrame.TSN_7_12
	*/

	is_arp_i_not_null[list_rim_frame.Link_ID/8] |= (128 >> (list_rim_frame.Link_ID%8));
	TreeNode2Field fictiveFrame;
	fictiveFrame.Link_ID = list_rim_frame.Link_ID;
	fictiveFrame.serialNumber = list_rim_frame.RIMFrame.TSN_7_12;
	/*writing into flash and after that deleting pointer which is pointed on created node*/
	//aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa start
	TreeNode *inserted_node = 0;
	insert_newNode(TC1_Get_us(), fictiveFrame, rootOfTree, &inserted_node);
	free(inserted_node);
	//aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa end
}
/*this function must return list_rim_frame from "Link_ID" row of ARP table*/
void read_from_arp(struct ListRIMFrames *list_rim_frame, char Link_ID)
{
	//this function must return list_rim_frame from "Link_ID" row of ARP table
	//also this function must add the needed fields into list_rim_frame
	//the needed fields are
	//1. list_rim_frame->Link_ID = index
	//2. list_rim_frame->isNULL = 0
	//eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
}
/*this function must delete "Link_ID" row of ARP table*/
void delete_from_arp(char Link_ID)
{
	/*
	//in this place we need to disconnect remote, and when remote
	//confirm that its already disconnected, we can continue executing program
	//this is the part of code, into which we must add code
	//while(remote is not disconnected){}//eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
	*/
	delete_treeNode_byLinkID(Link_ID);

	is_arp_i_not_null[Link_ID/8] &= ~(128 >> (Link_ID%8));
}
/*ARP end*/
